<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
	<head>
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<title>Unhandled Exception</title>
		<base href="<?= Phpr::$request->getRootUrl().Phpr::$request->getSubdirectory() ?>" />
		<link rel="stylesheet" type="text/css" href="modules/cms/resources/css/frontend_error.css" />
		
		<script type="text/javascript">
			var lemonstand_root_dir = '<?= Phpr::$request->getSubdirectory() ?>';
		</script>

		<script type="text/javascript" src="phproad/javascript/mootools.js"></script>
		<script type="text/javascript" src="modules/cms/resources/javascript/frontend_messages.js"></script>

		<script type="text/javascript" src="phproad/thirdpart/syntaxhighlighter/scripts/shCore.js"></script>
		<script type="text/javascript" src="phproad/thirdpart/syntaxhighlighter/scripts/shBrushPhp.js"></script>
		<script type="text/javascript" src="phproad/thirdpart/syntaxhighlighter/scripts/shBrushXml.js"></script>

		<link type="text/css" rel="stylesheet" href="phproad/thirdpart/syntaxhighlighter/styles/shCore.css"/>
		<link type="text/css" rel="stylesheet" href="phproad/thirdpart/syntaxhighlighter/styles/shThemeDefault.css"/>
		
		<script type="text/javascript">
			SyntaxHighlighter.all();
			SyntaxHighlighter.defaults.gutter = true;
		</script>
	</head>
	<body>
		
		<div id="content">
			<h1>Unhandled Error!</h1>
			
			<div id="wrapper">
				<p class="error"><?php echo Core_String::ucfirst(nl2br(Phpr_Html::Encode($Exception->getMessage()))) ?></p>
				
				<? if (isset($Exception->hint_message) && strlen($Exception->hint_message)): ?>
					<div class="hint">
						<h3>Hint</h3>
						<p><?= $Exception->hint_message ?></p>
					</div>
				<? endif ?>
				
				<table class="column_view">
					<tbody>
						<tr class="first">
							<th>Document</th>
							<td><?php
								if (!$Exception instanceof Cms_ExecutionException)
									echo Phpr_Files::rootRelative($Exception->getFile());
								else
									echo $Exception->document_name();
							?></td>
						</tr>
						<tr>
							<th>Document type</th>
							<td>
								<? if ($Exception instanceof Cms_ExecutionException): ?>
									<?= $Exception->document_type() ?>
								<? else: ?>
									PHP document
								<? endif ?>
							</td>
						</tr>
						<tr>
							<th>Line</th>
							<td><?= 
								$Exception instanceof Cms_ExecutionException ? 
									$Exception->code_line :
									$Exception->getLine()
							?></td>
						</tr>
						<tr class="last">
							<th>Exception class</th>
							<td><?php echo get_class($Exception) ?></td>
						</tr>
					</tbody>
				</table>
				
				<?
					$code_lines = null;
					
					if ($Exception instanceof Cms_ExecutionException)
					{
						$code_lines = explode("\n", $Exception->document_code());

						foreach ($code_lines as &$line)
							$line .= "\n";

						$error_line = $Exception->code_line-1;
					} else
					{
						$file = $Exception->getFile();
						if (file_exists($file) && is_readable($file))
						{
							$code_lines = @file($file);
							$error_line = $Exception->getLine()-1;
						}
					}
					
					if ($code_lines)
					{
						$start_line = $error_line-6;
						if ($start_line < 0)
							$start_line = 0;
							
						$end_line = $start_line + 12;
						$line_num = count($code_lines);
						if ($end_line > $line_num-1)
							$end_line = $line_num-1;

						$code_lines = array_slice($code_lines , $start_line, $end_line-$start_line+1);
					}

				?>
				
				<? if ($code_lines): ?>
					<div class="details">
						<h3>Code Highlight</h3>
						<div class="code"><pre class="<? if ($Exception instanceof Cms_ExecutionException): ?>brush:php;<? else: ?>brush:php;<? endif ?>toolbar:false;wrap-lines:false;tab-size:3;first-line:<?= $start_line+1 ?>;highlight:[<?= $error_line+1 ?>]"><?  
							foreach ($code_lines as $code_line) 
							{
								if (strlen(trim($code_line)))
									print h($code_line); 
								else
								 	print "&nbsp;\n";
							}
						?></pre></div>
					</div>
				<? endif ?>
				
				<div class="details last">
					<table class="call_stack">
						<? if ($Exception instanceof Cms_ExecutionException): ?>
							<thead>
								<tr>
									<th class="first record_number">#</th>
									<th>Document</th>
									<th class="last">Type</th>
								</tr>
							</thead>
							<tbody>
							<? 
								$last_index = count($Exception->call_stack) - 1;
								foreach ($Exception->call_stack as $index=>$stack_item): 
							?>
								<tr class="<?= Backend_Html::zebra('callstack') ?> <?= $index == $last_index ? 'last' : null ?>">
									<td class="first"><?= $last_index-$index+1 ?></td>
									<td><?= h($stack_item->name) ?></td>
									<td class="last"><?= h($stack_item->type) ?></td>
								</tr>
							<? endforeach ?>
							</tbody>
						<? else: ?>
							<thead>
								<tr>
									<th class="first record_number">#</th>
									<th>Call Stack Code</th>
									<th>Document</th>
									<th class="last">Line</th>
								</tr>
							</thead>
							<tbody>
								<?
									$trace_info = $Exception->getTrace();
									$last_index = count($trace_info) - 1;
									foreach ( $trace_info as $index=>$event ):
										$functionName = (isset($event['class']) && strlen($event['class'])) ? $event['class']."->".$event['function'] : $event['function'];

										if ( $functionName == 'Phpr_SysErrorHandler' || $functionName == 'Phpr_SysExceptionHandler' )
											continue;
										
										$file = isset($event['file']) ? Phpr_Files::rootRelative($event['file']) : null;
										$line = isset($event['line']) ? $event['line'] : null;

										$args = null;
										if ( isset($event['args']) && count($event['args']) )
											$args = Phpr_Exception::_formatTraceArguments($event['args'], false);
										
										if (strlen($args))
											$args = "<span class=\"arguments\">".h($args)."</span>";
								?>
								<tr class="<?= Backend_Html::zebra('callstack') ?> <?= $index == $last_index ? 'last' : null ?>">
									<td class="first"><?= $last_index-$index+1 ?></td>
									<td><?= $functionName."(".$args.")" ?></td>
									<td><?= $file ?></td>
									<td class="last"><?= $line ?></td>
								</tr>
								<? endforeach ?>
							</tbody>
						<? endif ?>
					</table>
				</div>
			</div>
		</div>
	</body>
</html>